package com.madding.shared.encrypt.cert.bc.util;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.madding.shared.encrypt.cert.exception.TDCertException;

public class KeyPairUtil {
    
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * Create a random 1024 bit RSA key pair
     */
    public static KeyPair generateRSAKeyPair() throws TDCertException {
        
        try {
            KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
            kpGen.initialize(1024, new SecureRandom());
            return kpGen.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            throw new TDCertException(e);
        } catch (NoSuchProviderException e) {
            throw new TDCertException(e);
        }
    }
}
